package com.viish.android.apps.buildbot;

/* Android Buildbot Manager
Copyright (C) 2011  Sylvain "Viish" Berfini

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.TableLayout;
import android.widget.TextView;

import com.viish.android.apps.buildbot.job.Build;
import com.viish.android.apps.buildbot.job.BuildbotJSONParser;
import com.viish.android.apps.buildbot.job.Builder;
import com.viish.android.apps.buildbot.job.Project;
import com.viish.android.apps.buildbot.job.Slave;

public class WaterfallActivity extends Activity
{
	private String host;
	private int port;
	
	private Project project;
	private List<Slave> slaves;
	private List<Builder> builders;
	private List<List<Build>> builds;
	
	public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.waterfall);
        setTitle("DEBUG View");
        
        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        host = prefs.getString("host", "");
        port = Integer.parseInt(prefs.getString("port", "8010"));
        if (! host.startsWith("http://"))
        	host = "http://" + host;
        
        BuildbotJSONParser parser = new BuildbotJSONParser(host, port);
        
        String debug = "Builders : ";
        builders = parser.getBuilders();
        for (Builder b : builders)
        	debug += b.toString() + "\n";
        
        debug += "\nSlaves : ";
        slaves = parser.getSlaves();
        for (Slave s : slaves)
        	debug += s.toString() + "\n";
        
        debug += "\nProject : ";
        project = parser.getProject();
        debug += project.toString() + "\n";
        
        debug += "\nLatests builds : \n";
        builds = new ArrayList<List<Build>>();
        for (Builder builder : builders)
        {
        	List<Build> b = parser.getBuilds(builder.getName());
        	builds.add(b);

        	// For debug purposes, we only display the last one
        	int max = -1;
        	Build maxB = null;
        	for (Build build : b)
        	{
        		if (build.getNumber() > max) {
        			max = build.getNumber();
        			maxB = build;
        		}
        	}
        	debug += maxB.toString() + "\n";
        }
        
        TextView tvDebug = (TextView) findViewById(R.id.debug);
        tvDebug.setText(debug);
        tvDebug.setTextColor(Color.BLACK);
        
//        drawWaterfall();
    }
	
	//TODO
	@SuppressWarnings("unused")
	private void drawWaterfall()
	{
		TableLayout waterfall = (TableLayout) findViewById(R.id.waterfall);
	}
}
